package cn.csxw.openchina.base;


import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.view.View;

import java.util.ArrayList;

import butterknife.BindView;
import cn.csxw.openchina.R;
import cn.csxw.openchina.ui.view.ChildViewPager;

/**
 * 这个是带有TabLayout+ViewPager的fragment基类
 */
public  abstract class BaseTabRefreshFragment extends BaseFragment {
    @BindView(R.id.tab_base_title)
    TabLayout mTabBaseTitle;
    @BindView(R.id.vp_base_content)
    ChildViewPager mVpBaseContent;

    private BaseContentPagerAdapter mAdapter;
    @Override
    public int getLayoutRes() {
        //把布局返回回去
        return R.layout.fragment_tab_refresh_base;
    }

    @Override
    public void initView() {
        initViewPagerAndTitle();
        ArrayList<BaseRefreshFragment> fragmentList = getFragmentList();
        String [] title  = getTitle();
        if(fragmentList!= null && fragmentList.size()>0){
            mAdapter.setSwipeList(fragmentList,title);
            mTabBaseTitle.setupWithViewPager(mVpBaseContent);
        }
    }
    //初始化ViewPager和标题
    private void initViewPagerAndTitle() {
        if(mAdapter== null) {
            //获取事务管理器
            //这个导致重大Bug，谨记，当fragment嵌套fragment的时候要用getChildFragmentManager();
          //  FragmentManager fm = mActivity.getSupportFragmentManager();
            FragmentManager fm = getChildFragmentManager();
             mAdapter = new BaseContentPagerAdapter(fm);
            mVpBaseContent.setAdapter(mAdapter);
        }
    }
    @Override
    public void initData() {
    }
    //提供方法让子类把标题传进来
    protected abstract String[] getTitle();
    //让子类把集合传进来
    protected abstract ArrayList<BaseRefreshFragment> getFragmentList() ;
    /////////////////////////////////////下面先留着
    @Override
    public void initListener() {

    }

    @Override
    public void onClick(View v, int id) {

    }
}
